package com.xbao.service.impl;

import com.xbao.core.util.MyBeanUtil;
import com.xbao.dao.StuAccountExtraMapper;
import com.xbao.dao.StuAccountMapper;
import com.xbao.model.StuAccount;
import com.xbao.model.StuAccountExample;
import com.xbao.model.req.StuAccountReq;
import com.xbao.model.vo.StuAccountVo;
import com.xbao.service.StuAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/30.
 */
@Service
public class StuAccountSeriviceImpl implements StuAccountService {
    @Resource
    private StuAccountMapper accountMapper;
    @Autowired
    private StuAccountExtraMapper extraMapper;

    @Override
    public void addStuAccount(StuAccountReq account) {
        accountMapper.insert(account);

    }

    @Override
    public StuAccountVo detailAccount(Long uid) {
        StuAccount account = accountMapper.selectByPrimaryKey(uid);
        StuAccountVo vo = MyBeanUtil.copyProperties(account, StuAccountVo.class);
        return vo;


    }

    @Override
    public StuAccountVo detailAccountAndSubject(StuAccountReq stuAccountReq) {
        StuAccountExample example = new StuAccountExample();
        StuAccountExample.Criteria criteria = example.createCriteria();
        criteria.andSidEqualTo(stuAccountReq.getSid());
        criteria.andSubidEqualTo(stuAccountReq.getSubid());
        List<StuAccount> list = accountMapper.selectByExample(example);
        if (list != null && !list.isEmpty()) {
            StuAccountVo vo = MyBeanUtil.copyProperties(list.get(0), StuAccountVo.class);
            return vo;
        }
        return null;
    }

    @Override
    public void deleteAccount(Long uid) {
        accountMapper.deleteByPrimaryKey(uid);

    }

    @Override
    public void charge(StuAccountReq stuAccountReq) {
        StuAccountVo detail = detailAccountAndSubject(stuAccountReq);
        if (detail == null) {
            addStuAccount(stuAccountReq);
        } else {
            recharge(stuAccountReq);
        }

    }

    @Override
    public void recharge(StuAccountReq stuAccountReq) {
        extraMapper.recharge(stuAccountReq);
    }


    @Override
    public void modifyAccount(StuAccountReq stuAccount) {
        accountMapper.updateByPrimaryKey(stuAccount);

    }

    @Override
    public List<StuAccountVo> queryAccount(StuAccountReq accountReq) {
        StuAccountExample example = new StuAccountExample();
        StuAccountExample.Criteria criteria = example.createCriteria();
        if (accountReq.getSid() != null) {
            criteria.andSidEqualTo(accountReq.getSid());
        }
        List<StuAccount> list = accountMapper.selectByExample(example);
        List<StuAccountVo> vos = MyBeanUtil.copyList(list, StuAccountVo.class);
        return vos;
    }

    @Override
    public List<StuAccountVo> queryUserAccouts(Long sid) {
        StuAccountExample example = new StuAccountExample();
        StuAccountExample.Criteria criteria = example.createCriteria();
        criteria.andSidEqualTo(sid);
        List<StuAccount> list = accountMapper.selectByExample(example);
        List<StuAccountVo> vos = MyBeanUtil.copyList(list, StuAccountVo.class);
        return vos;
    }

    @Override
    public StuAccount aggAccount(Long sid) {
        StuAccount stuAccount = extraMapper.aggAccount(sid);
        return stuAccount;


    }
}
